package com.fs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.mapper.SysAdminTokenMapper;
import com.fs.pojo.SysAdminToken;
import com.fs.service.SysAdminService;
import com.fs.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class BaseController {
    @Autowired
    private SysAdminTokenMapper adminTokenDao;

    public SysResult checkLogin(String cookie){//token=ba807f54-12e2-47fa-b558-cce493cb6828
        if(cookie==null || "".equals(cookie)){
            return SysResult.fail("请先登录");
        }

        String[] split = cookie.split("=");
        if(split.length!=2){
            return SysResult.fail("token有误");
        }

        Date now = new Date();
        String token=split[1];
        QueryWrapper<SysAdminToken> tokenEntityQueryWrapper = new QueryWrapper<>();
        tokenEntityQueryWrapper.eq("token", token)
                .gt("end_time", now);
        SysAdminToken tokenEntity = adminTokenDao.selectOne(tokenEntityQueryWrapper);
        if(tokenEntity==null){
            return SysResult.fail("token失效");
        }
        return SysResult.success(tokenEntity.getUserId());
    }
}
